/**
 * 2018年12月1日下午3:44:42
 * @author 谢进财
 */
package com.baihang.framework.kit;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 上传下载工具
 *
 */
public class DownLoadKit {
	@SuppressWarnings("resource")
	public static ResponseEntity<byte[]> downLoadFile(File file) throws Exception  {
        byte[] body = null;
        //获取文件
        FileInputStream is = new FileInputStream(file);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        //设置文件类型
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        //设置Http状态码
        HttpStatus statusCode = HttpStatus.OK;
        //返回数据
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
	/**
	 * 传统方法
	 * @param response
	 * @param file
	 * @throws Exception
	 */
    public static void downLoadFile2(HttpServletResponse response, File file) throws Exception {
    	response.setContentType("application/force-download");// 设置强制下载不打开
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        ServletOutputStream os = response.getOutputStream();//获得输出流对象
        os.flush();
        int i = bis.read(buffer);
        while (i != -1) {
            os.write(buffer, 0, i);//写入输出流
            i = bis.read(buffer);
        }
        bis.close();//关闭缓存流
        fis.close();//关闭输入流
        os.close();
    }
	/**
	 * 
	 * @param HSSFWorkbook wb
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static ResponseEntity<byte[]> exportExcel(HSSFWorkbook wb,String name) throws Exception{
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		wb.write(os);
        byte[] body = os.toByteArray();//获得字节数组
       
        HttpHeaders headers = new HttpHeaders();
        //设置文件类型
       // headers.add("Content-Disposition", "attchement;filename=" + name);
        headers.add("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xls", "UTF-8"));

        //设置Http状态码
        HttpStatus statusCode = HttpStatus.OK;
        //返回数据
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
	/**
	 * 传统的输出方式
	 * @param response
	 * @param wb
	 * @param name
	 * @throws Exception
	 */
	public static void exportExcel2(HttpServletResponse response,HSSFWorkbook wb,String name) throws Exception {
		response.setContentType("application/binary;charset=UTF-8");//设置返回浏览器类型说明，让浏览器安二进制的方式读取
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xls", "UTF-8"));
		ServletOutputStream out=response.getOutputStream();//获取输出流
		
		out.flush();
		wb.write(out);//将excel文件写入输出流
		out.close();
	}
	
}
